package com.junki.officecontentsearch.util;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * @author junki
 * @date 2021/5/31 10:19
 */
public final class OfficeUtil {

    private OfficeUtil() {
    }

    // 获取pdf内容
    public static String getPdfContent(MultipartFile file) throws IOException {

        // 创建pdf文档对象
        PDDocument pdDocument = PDDocument.load(file.getInputStream());
        // 创建pdf文档内容处理对象
        PDFTextStripper pdfTextStripper = new PDFTextStripper();
        // 读取pdf文档内容
        return pdfTextStripper.getText(pdDocument);
    }

    // 获取excel内容
    public static String getExcelContent(MultipartFile file) throws IOException {
        StringBuilder text = new StringBuilder();
        // 创建工作簿对象
        Workbook workbook = WorkbookFactory.create(file.getInputStream());
        // 获取sheet数量
        int numberOfSheets = workbook.getNumberOfSheets();
        // 遍历获取所有sheet
        for (int i = 0; i < numberOfSheets; i++) {
            // 获取sheet
            Sheet sheet = workbook.getSheet(workbook.getSheetName(i));
            if (sheet == null) {
                continue;
            }
            // 获取行数
            int physicalNumberOfRows = sheet.getPhysicalNumberOfRows();
            // 遍历所有行
            for (int j = 0; j < physicalNumberOfRows; j++) {
                // 获取行
                Row row = sheet.getRow(j);
                if (row == null) {
                    continue;
                }
                // 获取单元格数
                int physicalNumberOfCells = row.getPhysicalNumberOfCells();
                // 遍历所有单元格
                for (int k = 0; k < physicalNumberOfCells; k++) {
                    // 获取单元格
                    Cell cell = row.getCell(k);
                    if (cell == null) {
                        continue;
                    }
                    // 拼接内容
                    text.append(cell).append(" ");
                }
            }
        }
        // 关闭工作簿对象
        workbook.close();
        return text.toString();
    }

    // 获取word内容
    public static String getWordContent(MultipartFile file) throws IOException {
        String text = "";

        // doc和docx分别处理
        if (Objects.requireNonNull(file.getOriginalFilename()).endsWith(".doc")) {
            // 创建wordExtractor对象
            WordExtractor wordExtractor = new WordExtractor(file.getInputStream());
            // 获取内容
            text = wordExtractor.getText();
            // 关闭wordExtractor对象
            wordExtractor.close();
        } else if (Objects.requireNonNull(file.getOriginalFilename()).endsWith(".docx")) {
            // 创建document对象
            XWPFDocument document = new XWPFDocument(file.getInputStream());
            // 获取段落
            List<XWPFParagraph> paragraphs = document.getParagraphs();
            // 遍历内容
            StringBuilder textSb = new StringBuilder();
            for (XWPFParagraph paragraph : paragraphs) {
                textSb.append(paragraph.getText()).append("\n");
            }
            text = textSb.toString();
            // 关闭document对象
            document.close();
        }

        return text;
    }

}
